package com.prs.crm.dao.hr.impl;

import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.context.AppContext;
import com.prs.crm.dao.hr.TrainDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.Train;

public class TrainDaoImpl extends BaseDaoImpl<Train> implements TrainDao{

	@Override
	public Collection<Train> getTrainByPerson(Person person) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Train.class);
		criteria.add(Restrictions.eq("trainPerson", AppContext.getAccountContext().currentUser()));
		criteria.addOrder(Order.desc("recordTime"));
		return criteria.list();
	}

	@Override
	public Collection<?> getReportPersonTrainSource() {
		// TODO Auto-generated method stub
		return (Collection<?>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				
				Criteria criteria = arg0.createCriteria(Train.class);
				ProjectionList projectionlist=Projections.projectionList();  
	        	projectionlist.add(Projections.groupProperty("item"));
	        	projectionlist.add(Projections.rowCount());
	        	criteria.setProjection(projectionlist);
				return criteria.list();
			}
			
		});
	}

}
